// 
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/util/sz_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:whiteboard/whiteboard.dart';

/// 白板工具
class WhiteboardWidget extends StatefulWidget {
  /// 关闭白板的方法
  final Function() onTapFunClose;

  const WhiteboardWidget({super.key, required this.onTapFunClose});

  @override
  State<WhiteboardWidget> createState() => _WhiteboardWidgetState();
}

class _WhiteboardWidgetState extends State<WhiteboardWidget> {
  late BuildContext _context;

  /// 白板控制器
  final WhiteBoardController _whiteBoardController = WhiteBoardController();

  @override
  Widget build(BuildContext context) {
    debugPrint("whiteboardWidget 组件 -> build 方法执行 ===> ");
    debugPrint("");
    _context = context;
    return Column(
      children: [
        _titleWidget(),
        Expanded(child: _whiteboard()),
      ],
    );
  }

  /// 标题组件
  _titleWidget() {
    var top = MediaQuery.of(_context).padding.top;
    return Container(
      height: top + 88,
      padding: EdgeInsets.only(top: top, left: 30, right: 23),
      decoration: BoxDecoration(
        color: getColorWhite1(szYjms: szUtilComm.szInfo.yjms),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _iconItems(),
          _showIcon("images/back/back_close.svg", _onTapFunClose),
        ],
      ),
    );
  }

  /// 图标集合
  _iconItems() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        _showIcon("images/question/whiteboard_back.svg", _onTapFunBack),
        hiSpace(width: 48),
        _showIcon(
            "images/question/whiteboard_next_step.svg", _onTapFunNextStep),
        hiSpace(width: 48),
        _showIcon("images/subject/subject_trash_can.svg", _onTapFunClear),
      ],
    );
  }

  /// 显示对应图标方法
  _showIcon(String svgUrl, Function() onTapFun) {
    return GestureDetector(
      onTap: onTapFun,
      child: SvgPicture.asset(
        svgUrl,
        width: 40,
        height: 40,
        color: szUtilComm.szInfo.yjms ? colorWhite1 : null,
      ),
    );
  }

  /// 上一步方法
  _onTapFunBack() {
    debugPrint("whiteboardWidget 组件 -> 你点击了白板上一步方法 ===> ");
    var undo = _whiteBoardController.undo();
    debugPrint("whiteboardWidget 组件 -> undo ===> $undo");
  }

  /// 下一步方法
  _onTapFunNextStep() {
    debugPrint("whiteboardWidget 组件 -> 你点击了白板下一步方法 ===> ");
    var redo = _whiteBoardController.redo();
    debugPrint("whiteboardWidget 组件 -> redo ===> $redo");
  }

  /// 清空方法
  _onTapFunClear() {
    debugPrint("whiteboardWidget 组件 -> 你点击了白板清空方法 ===> ");
    _whiteBoardController.clear();
  }

  /// 关闭方法
  _onTapFunClose() {
    debugPrint("whiteboardWidget 组件 -> 你点击了关闭方法 ===> ");
    widget.onTapFunClose();
  }

  /// 白板工具
  _whiteboard() {
    return WhiteBoard(
      // 背景白板颜色
      backgroundColor: colorBlack7,
      // 用于在白板上操作的控制器
      controller: _whiteBoardController,
      // 手绘笔划宽度
      strokeWidth: 3,
      // 手绘的笔触颜色
      strokeColor: szUtilComm.szInfo.yjms ? colorRed8 : colorRed6,
      // 对于橡皮擦模式
      isErasing: false,
    );
  }
}
